<template>
  <com-card>
    <div slot="com_title">添加账号</div>
    <div slot="com_content">
      <el-row>
        <el-col :sm="8">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rules"
            ref="loginForm"
            label-width="80px"
            size="small"
          >
            <el-form-item label="用户名：" prop="account">
              <el-input v-model="ruleForm.account">
                <i slot="prefix" class="iconfont icon-yonghufill"></i>
              </el-input>
            </el-form-item>
            <el-form-item label="密码：" prop="password">
              <el-input
                :type="flag ? 'text' : 'password'"
                v-model="ruleForm.password"
              >
                <i slot="prefix" class="iconfont icon-mima"></i>
                <i
                  slot="suffix"
                  :class="[
                    'iconfont',
                    'yj',
                    flag
                      ? 'icon-yanjing_huaban1'
                      : 'icon-05wangluoanquan_biyanjing',
                  ]"
                  @click="flag = !flag"
                ></i>
              </el-input>
            </el-form-item>
            <el-form-item label="用户组：">
              <el-select
                placeholder="请选择用户组"
                v-model="ruleForm.userGroup"
              >
                <el-option value="超级管理员" label="超级管理员"></el-option>
                <el-option value="普通管理员" label="普通管理员"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" class="tijiao" @click="goLogin"
                >提交</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </com-card>
</template>

<script>
import ComCard from "../../components/comCard";
import { api_save } from "../../api/users";
export default {
  components: {
    ComCard,
  },
  data() {
    function checkAccount(rule, value, callback) {
      const reg = /^[0-9a-z]{3,12}$/gi;
      if (reg.test(value)) {
        callback();
      } else {
        {
          return callback(new Error("请输入3-12位字符"));
        }
      }
    }
    function checkPassword(rule, value, callback) {
      const reg = /^[a-z][0-9a-z]{2,11}$/gi;
      if (reg.test(value)) {
        callback();
      } else {
        {
          return callback(new Error("请输入以字母开头的3-12位字符"));
        }
      }
    }

    return {
      ruleForm: {
        account: "",
        password: "",
        userGroup: "超级管理员",
      },
      rules: {
        account: [{ validator: checkAccount, trigger: "blur" }],
        password: [{ validator: checkPassword, trigger: "blur" }],
      },
      flag: false,
    };
  },
  methods: {
    goLogin() {
      this.$refs.loginForm.validate(async (b) => {
        if (b) {
        let rst=await api_save(this.ruleForm);
        console.log(rst);
        let{code,msg}=rst;
        if(code==0){
            this.$router.push("/users/list");
        }else{
            this.$message.error(msg);
        }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
